package br.ufrj.dcc.manifestacoes.business;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import br.ufrj.dcc.manifestacoes.model.Comentario;
import br.ufrj.dcc.manifestacoes.util.DBUtil;
import br.ufrj.dcc.manifestacoes.util.FacebookUtil;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;

import facebook4j.Comment;
import facebook4j.Facebook;
import facebook4j.FacebookException;
import facebook4j.ResponseList;
import facebook4j.User;
import facebook4j.internal.org.json.JSONObject;

public class FacebookBusiness {
	

	public ArrayList<Comentario> saveComments(ResponseList<Comment> listacomentarios, DBCollection collection) {
		ArrayList<Comentario> comentarios = new ArrayList<Comentario>();
		for(Comment comment : listacomentarios){
			try {
				User comentarista = FacebookUtil.getConnection().getUser(comment.getFrom().getId());
				if(comentarista.getGender() != null){
					Comentario comentario = new Comentario(comment,comentarista.getGender());
					DBObject dbObject = (DBObject) JSON.parse(new JSONObject(comentario).toString());
					BasicDBObject query = new BasicDBObject(Comentario.FIELD_ID, dbObject.get(Comentario.FIELD_ID));
					collection.update(query, dbObject, true, false);
					System.out.println("Comentário " + comentario.getId() +" salvo.");
					comentarios.add(comentario);
				}
			} catch (FacebookException e) {
				System.out.println("Não foi possível obter o gênero do usuário");
				e.printStackTrace();
			}
		}
		return comentarios;
	}

	public void saveComentario(Comentario comentario, String collectionName) {
		DBCollection collection = DBUtil.getInstance().getDatabase().getCollection(collectionName);
		DBObject dbObject = (DBObject) JSON.parse(new JSONObject(comentario).toString());
		BasicDBObject query = new BasicDBObject(Comentario.FIELD_ID, dbObject.get(Comentario.FIELD_ID));
		collection.update(query, dbObject, true, false);
		System.out.println("Comentário " + comentario.getId() +" salvo.");
	}
	
	public void removeAllComments(String collectionName) {
		DBCollection collection = DBUtil.getInstance().getDatabase().getCollection(collectionName);
		collection.remove(new BasicDBObject());
	}
	
	public static void main(String[] args) {

		Facebook facebook = FacebookUtil.getConnection();
		ResponseList<Comment> listacomentarios = FacebookUtil.getComments(facebook,FacebookUtil.ID_POST);
		FacebookBusiness fbBusiness = new FacebookBusiness();
		fbBusiness.removeAllComments(DBUtil.COLLECTION_COMMENTS);
		
		//Salva comentarios com genero nulo
		fbBusiness.saveComments(listacomentarios,DBUtil.getCollection());
	}
	
	public static Date parseTimestamp(String date)  {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(Long.valueOf(date));
		return calendar.getTime();
	}
	
	public static Date parseDate(String date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
		try {	
			return dateFormat.parse(date);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}
}
